/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.*;

/**
 * <p>Project: javaseapp - ReaderDemo
 * <p>Powered by webrx On 2021-08-12 14:42:50
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class ReaderDemo {
    public static void main(String[] args) {
        //字节流读取ascii 文件内容 文本文件
        try (FileInputStream fis = new FileInputStream("user.txt")) {
            System.out.println(fis.available());//87字节
            File f = new File("user.txt");
            System.out.println(f.length()); //87个字节
            System.out.printf("%1$tF %1$tT%n", f.lastModified());

            //java 9 读取所有字节
            //System.out.println(new String(fis.readAllBytes()));

            byte[] buf = new byte[fis.available()];
            fis.read(buf);
            System.out.println(new String(buf));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("-------------------------------------------");
        //字符流Reader FileReader BufferedReader
        StringBuilder sbu = new StringBuilder();
        try (FileReader reader = new FileReader("user.txt")) {
            while (reader.ready()) {
                sbu.append((char) reader.read());
            }
            System.out.println(sbu);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //BufferedReader
        try (BufferedReader br = new BufferedReader(new FileReader("user.txt"))) {
            while (br.ready()) {
                System.out.println(br.readLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
